<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="author" content="Yb">
    <title>弹出框</title>
    <script src="../js/jquery-3.1.1.js"></script>
    <style>
        * {
            box-sizing: border-box;
        }
        
        body {
            margin: 0;
        }
        
        #content {
            height: 1500px;
            background-color: lightblue;
        }
        
        #overlay {
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.2);
            position: absolute;
            top: 0;
            left: 0;
            z-index: 999;
            display: none;
        }
        
        #alert {
            width: 400px;
            height: 200px;
            background-color: #fff;
            position: absolute;
            top: 100px;
            left: 100px;
            z-index: 1000;
            display: none;
        }
        
        #alert h2 {
            margin: 0;
            height: 40px;
            line-height: 40px;
            text-align: center;
            font-size: 16px;
            background-color: #ddd;
            cursor: move;
            /*禁止用户选择页面上的内容*/
            user-select: none;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
        }
        
        #alert h2 .close {
            display: inline-block;
            width: 30px;
            height: 30px;
            position: absolute;
            top: 5px;
            right: 5px;
            line-height: 30px;
            text-align: center;
            transition: all 0.5s linear;
        }
        
        #alert h2 .close:hover {
            cursor: pointer;
            background-color: #aaa;
        }
        
        #alert form {
            width: 100%;
            height: 160px;
            padding: 20px;
        }
        
        #alert div {
            text-align: center;
            line-height: 40px;
        }
        
        #alert .login {
            border: none;
            font-size: 16px;
        }
        
        #alert input {
            width: 260px;
            height: 30px;
        }
    </style>
</head>

<body>
    <!--内容区-->
    <div id="content"></div>
    <center><button type="button">点我</button></center>
    <!--遮罩层-->
    <div id="overlay"></div>
    <!--弹窗-->
    <div id="alert">
        <h2>
            <span>用户登录</span>
            <span class="close">&times;</span>
        </h2>
        <form action="javascript:;">
            <div><label for="">账号：<input type="text" placeholder="请输入账号"></label></div>
            <div><label for="">密码：<input type="password" placeholder="请输入密码"></label></div>
            <div><input class="login" type="submit" value="登录"></div>

        </form>
    </div>
</body>

</html>

<script>
    $(function () {
        // console.log($('#alert').width() + '' + $('#alert').innerWidth() + '' + $('#alert').outerWidth());

        // 遮罩层的大小永远都是100%
        setOverlayWH();
        function setOverlayWH() {
            // 整个页面的大小  可时页面的大小+滚动条的大小
            // var w = $(document).width();
            // var h = $(document).height();
            // console.log('w='+w+';h='+h);

            var w = $(window).width() + $(document).scrollLeft();
            var h = $(window).height() + $(document).scrollTop();
            // console.log('w='+w+';h='+h);

            $('#overlay').width(w).height(h);
        }
        // 弹窗第一次出现居中，当浏览器窗口发生变化时也要居中
        setAlertCenter();
        function setAlertCenter() {
            // top = (可时页面的高 - 弹窗的高)/2 + 滚动条的距离
            var left = ($(window).width() - $('#alert').width()) / 2 + $(document).scrollLeft();
            var top = ($(window).height() - $('#alert').height()) / 2 + $(document).scrollTop();

            $('#alert').css({ left: left, top: top });
        }


        // 点击显示遮罩层和弹窗
        $('button').click(function () {
            $('#overlay').show();
            $('#alert').show();
            // 禁用滚动条
            $('html').css('overflow', 'hidden');
            $('body').css('overflow', 'hidden');
        });
        $('#alert .close').click(function () {
            $('#overlay').hide();
            $('#alert').hide();
            // 使用滚动条
            $('html').css('overflow', 'auto');
            $('body').css('overflow', 'auto');
        });

        // 当页面尺寸发生变化或者发生滚动时，重新计算尺寸
        $(window).resize(function () {
            setOverlayWH();
            setAlertCenter();
        });
        $(window).scroll(function () {
            setOverlayWH();
            setAlertCenter();
        });



        // 让弹窗移动
        $('#alert h2').mousedown(function (ev) {
            // 当按下鼠标时，获取当前点击的位置，距离元素左上角的偏移量
            var distanceX = ev.offsetX;
            var distanceY = ev.offsetY;
            $(document).mousemove(function (ev) {
                // console.dir(ev.pageX);
                // 不管点击h2的哪个位置，始终获取h2上鼠标点击的偏移量
                var left = ev.pageX - distanceX;
                var top = ev.pageY - distanceY;

                // 左右不能越界
                if (left < 0) {
                    left = 0;
                }
                if (left <= $(document).scrollLeft()) {
                    left = $(document).scrollLeft();
                }
                if (left >= $(window).width()-$('#alert').width()+$(document).scrollLeft()) {
                    left = $(window).width()-$('#alert').width()+$(document).scrollLeft();
                }

                // 上下不能越界
                if (top < 0) {
                    top = 0;
                }
                if (top <= $(document).scrollTop()) {
                    top = $(document).scrollTop();
                }
                if (top >= $(window).height()-$('#alert').height()+$(document).scrollTop()) {
                    top = $(window).height()-$('#alert').height()+$(document).scrollTop();
                }
                $('#alert').css({ left: left, top: top });
            });
        });

        // 松开鼠标，取消移动
        $(document).mouseup(function () {
            $(this).off('mousemove');
        })
    })

</script>